#ifndef _I_OLE_TIMER_H_
#define _I_OLE_TIMER_H_


#include <map>
#include <atlbase.h>
#include <atlcom.h>
#include <ocmm.h>

static GUID CLSID_IOleTimer;


class COleTimer :	
	public CComObjectRootEx<CComSingleThreadModel>,
	public CComCoClass<COleTimer>,
	public ITimer
{
public:
	COleTimer()
	{
		m_bFreeze = FALSE;
		m_dwCookie = 0;
	}
	virtual ~COleTimer()
	{
		if (m_dwCookie)
		{
			Unadvise(m_dwCookie);
		}
	}

	static HRESULT WINAPI UpdateRegistry(BOOL bRegister){return S_OK;};

	BEGIN_COM_MAP(COleTimer)
		COM_INTERFACE_ENTRY(ITimer)
	END_COM_MAP( )

	DECLARE_PROTECT_FINAL_CONSTRUCT()

	HRESULT FinalConstruct()
	{
		return S_OK;
	}

	void FinalRelease()
	{
	}


protected:
	BOOL		m_bFreeze;
	DWORD       m_dwCookie;

public:

	struct tagTimerInfo 
	{
		DWORD vtimeMin;
		DWORD vtimeMax;
//		CComPtr<ITimerSink >pTimerSink;
		ITimerSink* pTimerSink;
		COleTimer*	pTimer;

		tagTimerInfo()
		{
			vtimeMin = 0;
			vtimeMax = 0;
			pTimerSink = NULL;
			pTimer =  NULL;
		}
	};


	void OnTimer(int nId, ITimerSink* pSink);
	STDMETHOD (Advise) ( VARIANT vtimeMin,VARIANT vtimeMax,VARIANT vtimeInterval,DWORD dwFlags,ITimerSink *pTimerSink,DWORD *pdwCookie);
	STDMETHOD (Unadvise)( DWORD dwCookie) ;
	STDMETHOD (Freeze)( BOOL fFreeze) ;
	STDMETHOD (GetTime)(  VARIANT *pvtime);


	static VOID CALLBACK TimerObj_TimerProc(HWND hwnd,UINT uMsg,UINT_PTR idEvent,DWORD dwTime);

	static std::map<DWORD, tagTimerInfo> s_mapInfo;
};

OBJECT_ENTRY_AUTO(CLSID_IOleTimer, COleTimer)

#endif